#include "camera.h"
#include "ui_camera.h"
//调用摄像头
#include <QCameraInfo>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QPixmap>
#include<QImage>
#include <QDebug>
#include"ChatWidget.h"

//弹窗
#include <QMessageBox>

camera::camera(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::camera)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach(const QCameraInfo &cameraInfo, cameras) {
        qDebug() << "CameraInfo:" << cameraInfo;
    }
    //添加内容
       camera1 = new QCamera(this);
       viewfind = new QCameraViewfinder();
      viewfind->show();
       camera1->setViewfinder(viewfind);
       imageCapture = new QCameraImageCapture(camera1);
       camera1->start();

       connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(on_imageCaptured(int, QImage)));

}

camera::~camera()
{
    delete ui;
}

void camera::on_imageCaptured(int id, const QImage &preview){
    ui->label->setPixmap(QPixmap::fromImage(preview));
}

void camera::on_pushButton1_clicked()
{
      qDebug()<<"ScreenShot";

    //截取用户打卡时的图片

    imageCapture->capture();
    QMessageBox::information(this, "Camera", "拍照成功");
}

void camera::on_pushButton_clicked()
{
    this->close();


}

void camera::mousePressEvent(QMouseEvent *event)
{
    //event->globalPos()获取鼠标以电脑屏幕左上角为原点的向量
    if(event->button()==Qt::LeftButton)
    {
        this->m_moveing2=true;
        m_lastPos=event->globalPos()-pos();//使用向量的减法获得鼠标相对于窗口的位置

    }
    return  QWidget::mousePressEvent(event);

}

void camera::mouseMoveEvent(QMouseEvent *event)
{
    //在mouseMoveEvent 里event->button()返回的引进事件的button永远是Qt::NoButton!!
    if(m_moveing2 && (event->buttons()&&Qt::LeftButton))//左键被按下
    {
        move(event->globalPos()-m_lastPos);
       //m_lastPos=event->globalPos()-pos();//使用向量的减法获得鼠标相对于窗口的位置
    }
   return QWidget::mouseMoveEvent(event);
}

void camera::mouseReleaseEvent(QMouseEvent *event)
{
   Q_UNUSED(event);
   m_moveing2=false;
}
